/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models.negocio;

import config.LogErro;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import models.dados.HibernateDAO;
import models.dados.HibernateUtil;
import models.entidades.AtributoTabAux;
import models.entidades.TabAux;
import org.hibernate.Query;
import views.utilitario.AtributosTabAuxSem;
import views.utilitario.ReferenciaAtributos;

/**
 *
 * @author Wenderson
 */
public class NegocioTabAuxiliar extends HibernateDAO<TabAux, Long> {

    private enum Auditoria {CHAVE_NATURAL, DT_CARREGAMENTO};
    
    public NegocioTabAuxiliar() {
        super(TabAux.class);
    }
    
    public boolean inserir(ArrayList<AtributosTabAuxSem> tabAux) {
        try {
            HibernateUtil.beginTransaction();
            
            TabAux tAux = new TabAux();
            tAux.setEsquema(tabAux.get(0).getSchema());
            tAux.setNome(tabAux.get(0).getNomeTabela());
            
            HibernateUtil.getSession().save(tAux);
            
            HibernateUtil.getSession().flush();
            
            System.out.println("CD TAB AUXILIAR: " + tAux.getCdTabAux());
            
            for (AtributosTabAuxSem atrSem : tabAux) {
                AtributoTabAux atrAux = atrSem.getAtributoAux();
                atrAux.setTabAux(tAux);
                
                HibernateUtil.getSession().save(atrAux);
                
                HibernateUtil.getSession().flush();
                
                System.out.println("CD AT TAB AUX: " + atrAux.getCdAtTabAux());
                
                if (atrAux.getCdAtTabAux() == -1) {
                    return false;
                }
                
                if (atrSem.isChaveNatural()) {
                    this.inserirAuditoria(tAux.getCdTabAux(), atrAux.getCdAtTabAux(), Auditoria.CHAVE_NATURAL);
                }
                
                if (atrSem.isDataCarga()) {
                    this.inserirAuditoria(tAux.getCdTabAux(), atrAux.getCdAtTabAux(), Auditoria.DT_CARREGAMENTO);
                }
            }
            
            HibernateUtil.commitTransaction();
            
            return true;
        } catch (Exception e) {
            HibernateUtil.rollBackTransaction();
            LogErro.gravaErro(e);e.printStackTrace();
        }
        return false;
    }
    
    private boolean inserirAuditoria(int cd_tab_aux, int cd_at_tab_aux, Auditoria auditoria) throws Exception {
        String tabela = "";
        
        switch (auditoria) {
            case CHAVE_NATURAL: tabela = "CHAVE_NATURAL_AUX"; break;
            case DT_CARREGAMENTO: tabela = "DT_CARREGAMENTO"; break;
        }
        
        try {
            Query query = HibernateUtil.getSession().createSQLQuery("INSERT INTO " + tabela + " (CD_TAB_AUX, CD_AT_TAB_AUX) VALUES (?,?)");
            query.setInteger(0, cd_tab_aux);
            query.setInteger(1, cd_at_tab_aux);

            int resultado = query.executeUpdate();
            
            if (resultado > 0) {
                return true;
            }

        } catch (Exception e) {
            LogErro.gravaErro(e);e.printStackTrace();
        }
        throw new Exception("Erro ao inserir auditoria");
    }
     
    public ArrayList<ReferenciaAtributos> getAtributosAux(int codigo_tabela) {
        try {
            ArrayList<ReferenciaAtributos> lista = new ArrayList<ReferenciaAtributos>();
            
            //HibernateUtil.getSession().beginTransaction();
            
            TabAux tabAux = (TabAux) HibernateUtil.getSession().load(TabAux.class, codigo_tabela);
            
            Collection c = tabAux.getAtributoTabAuxes();
            
            for (Iterator it = c.iterator(); it.hasNext();) {
                AtributoTabAux atr = (AtributoTabAux)it.next();
                
                ReferenciaAtributos ref = new ReferenciaAtributos();
                ref.setCodigo(atr.getCdAtTabAux());
                ref.setNome(atr.getNome());
                ref.setTipo(atr.getTipoDado());
                
                lista.add(ref);
            }

            return lista;
        } catch (Exception e) {
            LogErro.gravaErro(e);e.printStackTrace();
        }
        return null;
    }
}
